/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年9月8日
 */
package com.massyframework.beanskin.license.signature.wizard;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * 向导
 * @author huangkh
 *
 */
public class Wizard {

	private Stage stage;
	private SignatureData data;
	private List<Form> forms;
	private int formIndex = 0;
	
	/**
	 * 构造方法
	 */
	public Wizard(Stage stage) {
		this.stage = Objects.requireNonNull(stage, "\"stage\" cannot be null.");
		this.data = new SignatureData();
		
		this.forms = new ArrayList<>();
		forms.add(new Form("bindCustomer"));
		forms.add(new Form("selectModule"));
		forms.add(new Form("selectCertificate"));
		forms.add(new Form("signature"));
		forms.add(new Form("signatureOutput"));
	}
	
	/**
	 * 舞台
	 * @return {@link Stage}
	 */
	public Stage getStage() {
		return this.stage;
	}
	
	/**
	 * 签名数据
	 * @return {@link SignatureData}
	 */
	public SignatureData getSignatureData() {
		return this.data;
	}
	
	public void start() {
		this.doNext();
	}
		
	/**
	 * 下一步
	 */
	protected void doNext() {
		if (this.formIndex < this.forms.size()) {
			Form form = this.forms.get(this.formIndex);
			this.formIndex ++ ;
			Scene scene = form.getScene();
			
			Controller controller = form.getFXMLLoader().getController();
			controller.beforeShow();
			this.stage.setScene(scene);
			
		}
		
	}
	
	/**
	 * 上一步
	 */
	protected void doPrevious() {
		if (this.formIndex != 0) {
			this.formIndex -- ;
			Form form = this.forms.get(this.formIndex -1);
			Scene scene = form.getScene();
			
			Controller controller = form.getFXMLLoader().getController();
			controller.beforeShow();
			this.stage.setScene(scene);
		}
	}
	
	
	private class Form {
		private String name;
		private FXMLLoader loader;
		private Scene scene;
		
		public Form (String name) {
			this.name = Objects.requireNonNull(name, "\"name\" cannot be null.");
		}
				
		/**
		 * 获取FXMLLoader
		 * @return {@link FXMLLoader}
		 */
		public synchronized FXMLLoader getFXMLLoader() {
			if (this.loader == null) {
				try {
					URL url = this.getClass().getClassLoader()
							.getResource("fxml/".concat(this.name).concat(".fxml"));
					if (url == null) {
						System.err.println(url);
					}
					this.loader=new FXMLLoader(url);
					this.scene = new Scene(this.loader.load());
					
					Controller controller = this.loader.getController();
					controller.init(Wizard.this);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
			
			return this.loader;
		}
		
		public Scene getScene() {
			if (this.scene == null) {
				synchronized(this) {
					if (this.scene == null) {
						this.getFXMLLoader();
					}
				}
			}
			
			return this.scene;
		}
	}
}
